package com.glc.hms.common.util;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 公共工具类
 * 存放一些散乱的工具类方法
 */
public class CommonUtil {

    /**
     * 拼接url后缀
     * @param url
     * @param suffix
     * @return
     */
    public static String insertFileNameSuffixToUrl(String url, String suffix){
        int index = url.lastIndexOf('.');
        if(index > 0){
            StringBuilder sb = new StringBuilder(url);
            sb.insert(index, suffix);
            return sb.toString();
        }
        return url;
    }

    /**
     * 拆分list ,将大list 拆成指定长度的小list
     * @param datas 源list
     * @param splitSize 小list指定长度
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> spliceArrays(List<T> datas, int splitSize) {
        if (datas == null || splitSize < 1) {
            return  null;
        }
        int totalSize = datas.size();
        int count = (totalSize % splitSize == 0) ?
                (totalSize / splitSize) : (totalSize/splitSize+1);

        List<List<T>> rows = new ArrayList<>();

        for (int i = 0; i < count;i++) {

            List<T> cols = datas.subList(i * splitSize,
                    (i == count - 1) ? totalSize : splitSize * (i + 1));
            rows.add(cols);
            System.out.println(cols);
        }
        return rows;
    }

    /**
     * 获取匿名手机号
     * 如:152****4799
     * @param phoneNum
     * @return
     */
    public static String getAnonymityPhoneNum(String phoneNum) {
        return phoneNum.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
    }


    /**
     * 获取匿名
     * 如:李**
     * @param name
     * @return
     */
    public static String getAnonymityName(String name) {
        if (name.length() > 1) {
            return name.subSequence(0, 1) + "**";
        }
        return name;

    }

}